package com.pctx.bibadevicemanager.service.impl;

import com.pctx.bibacommon.result.Result;
import com.pctx.bibacommon.util.DateTimeUtil;
import com.pctx.bibacommon.util.UuidUtil;
import com.pctx.bibadevicemanager.dao.DeviceInfoMapper;
import com.pctx.bibadevicemanager.pojo.DeviceInfo;
import com.pctx.bibadevicemanager.pojo.DeviceInfoExample;
import com.pctx.bibadevicemanager.service.DeviceInfoService;
import com.pctx.bibadevicemanager.service.DeviceUserService;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by chason on 2018/2/28.19:06
 */
@Service("deviceInfoService")
public class DeviceInfoServiceImpl implements DeviceInfoService {
    @Autowired
    private DeviceInfoMapper deviceInfoMapper;
    @Autowired
    private DeviceUserService deviceUserService;

    @Override
    public List<DeviceInfo> getByCompanyUuid(String companyUuid) throws Exception {
        if (StringUtils.isEmpty(companyUuid))
            return null;

        DeviceInfoExample example = new DeviceInfoExample();
        DeviceInfoExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyUuidEqualTo(companyUuid);

        return deviceInfoMapper.selectByExample(example);
    }

    @Override
    public DeviceInfo getByUuid(String uuid) throws Exception {
        if (StringUtils.isEmpty(uuid))
            return null;

        DeviceInfoExample example = new DeviceInfoExample();
        DeviceInfoExample.Criteria criteria = example.createCriteria();
        criteria.andUuidEqualTo(uuid);

        List<DeviceInfo> list = deviceInfoMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list))
            return null;

        return list.get(0);
    }

    @Override
    public boolean add(DeviceInfo info) throws Exception {
        if (null == info)
            return false;

        info.setUuid(UuidUtil.randomUUID());
        info.setCreateTime(DateTimeUtil.nowDate());
        info.setUpdateTime(DateTimeUtil.nowDate());

        return deviceInfoMapper.insertSelective(info) > 0;
    }

    @Override
    public boolean updateByUuid(DeviceInfo info) throws Exception {
        if (null == info || StringUtils.isEmpty(info.getUuid()))
            return false;

        DeviceInfo old = getByUuid(info.getUuid());
        if (null == old)
            return false;

        info.setId(old.getId());
        info.setUpdateTime(DateTimeUtil.nowDate());

        return deviceInfoMapper.updateByPrimaryKeySelective(info) > 0;
    }

    @Override
    public Result addByImei(String imei, String userUuid, String companyUuid, String deviceName) throws Exception {
        if (StringUtils.isEmpty(imei) || StringUtils.isEmpty(userUuid) || StringUtils.isEmpty(companyUuid))
            return Result.error("参数不能为空");

        if (imei.indexOf("-") < 0)
            return Result.error("imei格式不正确");

        String companyNo = imei.substring(0, imei.indexOf("-"));
        String deviceNo = imei.substring(imei.indexOf("-") + 1);

        DeviceInfo deviceInfo = getByDeviceNo(deviceNo);
        if (null != deviceInfo && StringUtils.isNotEmpty(deviceInfo.getUuid()))
            return Result.error("该设备已经被绑定");

        if (null == deviceInfo) {
            deviceInfo = new DeviceInfo();
            deviceInfo.setUuid(UuidUtil.randomUUID());
            deviceInfo.setCreateTime(DateTimeUtil.nowDate());
            deviceInfo.setUpdateTime(DateTimeUtil.nowDate());
        }

        deviceInfo.setName(deviceName);
        deviceInfo.setCompanyNo(companyNo);
        deviceInfo.setUserUuid(userUuid);
        deviceInfo.setDeviceNo(deviceNo);
        deviceInfo.setCompanyUuid(companyUuid);

        boolean flag = deviceInfoMapper.insertSelective(deviceInfo) > 0;

        // 添加设备用户关联关系
        if (flag){
            deviceUserService.add4Owner(deviceInfo.getUuid(),userUuid);
        }

        if (flag)
            return Result.success(deviceInfo, "添加成功");
        else
            return Result.error("添加失败");
    }

    @Override
    public DeviceInfo getByDeviceNo(String deviceNo) throws Exception {
        if (StringUtils.isEmpty(deviceNo))
            return null;

        DeviceInfoExample example = new DeviceInfoExample();
        DeviceInfoExample.Criteria criteria = example.createCriteria();
        criteria.andDeviceNoEqualTo(deviceNo)
                .andStatusNotEqualTo(DeviceInfo.STATUS_DEL);

        List<DeviceInfo> list = deviceInfoMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list))
            return null;
        return list.get(0);
    }

    /**
     * 获取用户设备列表，包括被分享的设备
     *
     * @param userUuid
     * @return
     * @throws Exception
     */
    @Override
    public Result getListByUserUuid(String userUuid, String companyUuid) throws Exception {
        if (StringUtils.isEmpty(userUuid) || StringUtils.isEmpty(companyUuid)) {
            return Result.error("参数不能为空");
        }

        List<Map> list = deviceInfoMapper.getListByUserUuid(userUuid,companyUuid);
        return Result.success(list);
    }


}
